package com.du.cloud.common.base.utils;


import com.du.cloud.common.base.constant.enums.ResultCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.Serializable;

/**
 * Desc:
 * Author: dushuang
 * Date 2019/12/30 17:03
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private int code;
    private String msg;
    private T data;
    private long time;

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("admin"));

    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.time = System.currentTimeMillis()/1000;
    }

    public static <T> Result<T> ok(T data) {
        return new Result<T>(ResultCode.SUCCESS.code, ResultCode.SUCCESS.message, data);
    }
    public static <T>Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> fail(T data) {
        return new Result<T>(ResultCode.FAILED.code, ResultCode.FAILED.message, data);
    }
    public static <T> Result <T> fail(String msg) {
        return new Result<T>(ResultCode.FAILED.code, msg, null);
    }
    public static <T> Result <T> fail() {
        return fail(null);
    }
}
